package com.tianqu.tms.www.transfer.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.www.transfer.actionform.CarOwnerAuditActionForm;
import com.tianqu.tms.www.transfer.viewhelper.CarOwnerAuditInputViewHelper;

public class CarOwnerAuditInputController extends TmsController {
	private Logger logger = Logger.getLogger(CarOwnerAuditInputController.class);

	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = getTxController();
		try {
			controller.begin();
			Codes code = Codes.getInstance();
			
			TransferAcceptedInfo acceptedInfo = TransferAcceptedInfo.getInstance();
			List<TransferAcceptedInfo> transferAcceptedInfoList = new ArrayList<TransferAcceptedInfo>();
			CarOwnerAuditInputViewHelper viewHelper = new CarOwnerAuditInputViewHelper();

			// 点击查询进行审核
			CarOwnerAuditActionForm inputForm = (CarOwnerAuditActionForm)uc.getInputForm();
			String serial = inputForm.getSerial();
			viewHelper.setSerial(serial);
			transferAcceptedInfoList = acceptedInfo.findListBySerial(serial);
			
			if (transferAcceptedInfoList == null || transferAcceptedInfoList.size() == 0) {
				// 0件 页面提示“【U0039】查询的结果不存在。
				uc.setAttribute("auditErr", "U0039");
			} else if (transferAcceptedInfoList != null && transferAcceptedInfoList.size() > 1) {
				// 复数件 页面提示“【U1405】出现重复流水号业务，请联系系统管理员。
				uc.setAttribute("auditErr", "U1405");
			} else {
				// 1件
				TransferAcceptedInfo transferAcceptedInfo = transferAcceptedInfoList.get(0);
				int transferRegister = transferAcceptedInfo.getTransferRegister();
				if (transferRegister == 0 || transferRegister == 2 ) {
					// 如果有符合条件数据 并且 公安审核（transferRegister）为“2：公安未审核”，那么页面提示“【U1064】该业务未通过公安审核，不能进行车主审核。”
					uc.setAttribute("auditErr", "U1064");
				} else if(transferRegister == 3) {
					//# 如果有符合条件数据 并且 公安审核（transferRegister）为“3：公安审核未通过”，那么页面提示“该业务公安审核未通过，原因：{原因}”
					uc.setAttribute("auditErr", "U1064A");
					Map<String, Integer> map = acceptedInfo.getTransferRegisterInfo(transferAcceptedInfo.getSerial());
					StringBuffer info = new StringBuffer();
					if(map.containsKey("epStandard") && map.get("epStandard") == 2) info.append(" 环保达标:未达标 ");
					if(map.containsKey("carReplaceType") &&map.get("carReplaceType") == 2) info.append(" 客车换型:换 ");
					if(map.containsKey("carPeccancy") &&map.get("carPeccancy") == 2) info.append(" 车辆违章:违章 ");
					if(map.containsKey("overdue") &&map.get("overdue") == 2) info.append(" 超检审核:车辆检验逾期 ");
					if(map.containsKey("mortgage") &&map.get("mortgage") == 2) info.append(" 抵押审核:已抵押 ");
					if(map.containsKey("sealup") &&map.get("sealup") == 2) info.append(" 查封审核:查封 ");
					if(map.containsKey("registration") &&map.get("registration") == 2) info.append(" 登记证书:无 ");
					if(map.containsKey("drivingLicense") &&map.get("drivingLicense") == 2) info.append(" 行驶证:不符 ");
					if(map.containsKey("scrapOverdue") &&map.get("scrapOverdue") == 2) info.append(" 报废超期:超期 ");
					if(map.containsKey("robbery") &&map.get("robbery") == 2) info.append(" 盗抢审核:盗抢 ");
					if(map.containsKey("ownerEscaping") &&map.get("ownerEscaping") == 2) info.append(" 原车主在逃:在逃 ");
					if(map.containsKey("ownerScrap") &&map.get("ownerScrap") == 2) info.append(" 原车主报废车:强制报废 ");
					if(map.containsKey("ownerLive") &&map.get("ownerLive") == 2) info.append(" 原车主居住证:无居住证明 ");
					if(map.containsKey("newEscaping") &&map.get("newEscaping") == 2) info.append(" 新车主在逃:在逃 ");
					if(map.containsKey("newScrap") &&map.get("newScrap") == 2) info.append(" 新车主报废车:强制报废 ");
					if(map.containsKey("newLive") &&map.get("newLive") == 2) info.append(" 新车主居住证:无居住证明 ");
					uc.setAttribute("auditInfo", info.toString());
				} else {
					
					//a）当该业务的公安审核（transferRegister）为“4：公安审核中”时，页面提示“【U1065】该业务在公安审核中，仅可以保存，不可以通过。”，同时，页面按钮显示，“上一步”、“保存”，隐藏“审核完成”。
					//b）当该业务的公安审核（transferRegister）为“1：公安审核通过”时，页面提示“【U1066】该业务在公安审核已通过。”，同时，页面按钮显示，“上一步”、“审核完成”，隐藏“保存”。

					if (transferRegister == 4)
						// 当该业务的公安审核（transferRegister）为“4：公安审核中”时，页面提示“【U1072】该业务在公安审核中，仅可以保存，不可以通过。”，同时，页面按钮显示，“上一步”、“保存”，隐藏“审核完成”。
						uc.setAttribute("auditErr", "U1072");
					
					if (transferRegister == 1)
						// 当该业务的公安审核（transferRegister）为“1：公安审核通过”时，页面提示“【U1071】该业务在公安审核已通过。”，同时，页面按钮显示，“上一步”、“审核完成”，隐藏“保存”。
						uc.setAttribute("auditErr", "U1071");
					
					viewHelper.setSerial(transferAcceptedInfo.getSerial());
					viewHelper.setVin(transferAcceptedInfo.getVin());
					viewHelper.setLicensePlate(transferAcceptedInfo.getLicensePlate());
					viewHelper.setMemberId(transferAcceptedInfo.getMemberId());
					viewHelper.setTransferType(transferAcceptedInfo.getTransferType());
					viewHelper.setTransferTypeName(code.getItemValueByItemIdAndItemCode(Codes.TRANSFER_TYPE, transferAcceptedInfo.getTransferType()));
					if (!StringUtil.isNullOrBlank(transferAcceptedInfo.getShiftToLevel())) 
						viewHelper.setShiftToLevel("（现行转入要求：" + transferAcceptedInfo.getShiftToLevel() + "）");
					viewHelper.setCarMarketInProv(transferAcceptedInfo.getCarMarketInProv());
					viewHelper.setCarMarketInCity(transferAcceptedInfo.getCarMarketInCity());
					viewHelper.setMaterialImages1(transferAcceptedInfo.getMaterialImages1());
					viewHelper.setMaterialImages2(transferAcceptedInfo.getMaterialImages2());
					viewHelper.setMaterialImages3(transferAcceptedInfo.getMaterialImages3());
					viewHelper.setMaterialImages4(transferAcceptedInfo.getMaterialImages4());
					viewHelper.setMaterialImages5(transferAcceptedInfo.getMaterialImages5());
					viewHelper.setMaterialImages6(transferAcceptedInfo.getMaterialImages6());
					viewHelper.setMaterialImages7(transferAcceptedInfo.getMaterialImages7());
					viewHelper.setMaterialImages8(transferAcceptedInfo.getMaterialImages8());
					viewHelper.setMaterialImages9(transferAcceptedInfo.getMaterialImages9());
					viewHelper.setMaterialImages10(transferAcceptedInfo.getMaterialImages10());
					viewHelper.setProName(transferAcceptedInfo.getProName());
					viewHelper.setCityName(transferAcceptedInfo.getCityName());
					viewHelper.setCarOwner(transferAcceptedInfo.getCarOwner());
					viewHelper.setCarOwnerIdCardNo(transferAcceptedInfo.getCarOwnerIdCardNo());
					viewHelper.setOwnerPhone(transferAcceptedInfo.getOwnerPhone());
					viewHelper.setOwnerAddress(transferAcceptedInfo.getOwnerAddress());
					viewHelper.setAgentName(transferAcceptedInfo.getAgentName());
					viewHelper.setAgentOwnerIdCardNo(transferAcceptedInfo.getAgentOwnerIdCardNo());
					viewHelper.setAgentAddress(transferAcceptedInfo.getAgentAddress());
					viewHelper.setAgentPhone(transferAcceptedInfo.getAgentPhone());
					viewHelper.setOwnerType(transferAcceptedInfo.getOwnerType());
					viewHelper.setOwnerOrgNo(transferAcceptedInfo.getOwnerOrgNo());
					viewHelper.setChangeBody(transferAcceptedInfo.getChangeBody());
					viewHelper.setChangeBodyName(code.getItemValueByItemIdAndItemCode(Codes.CHANGE_BODY_TYPE, transferAcceptedInfo.getChangeBody()));
					
					viewHelper.setTransferRegister(transferAcceptedInfo.getTransferRegister());
					// 1：公安审核通过、2：公安未审核、3：公安审核未通过、4：公安审核中
					switch (transferAcceptedInfo.getTransferRegister()){
						case 1:
							viewHelper.setTransferRegisterName("公安审核通过");
							break;
						case 2:
							viewHelper.setTransferRegisterName("公安未审核");
							break;
						case 3:
							viewHelper.setTransferRegisterName("公安审核未通过");
							break;
						case 4:
							viewHelper.setTransferRegisterName("公安审核中");
							break;
						case 5:
							viewHelper.setTransferRegisterName("有违章");
							break;
						default:
							viewHelper.setTransferRegisterName("");
					}

					if (transferAcceptedInfo.getCarPeccancy() == 1) {
						viewHelper.setCarPeccancyName("正常");
					}
					if (transferAcceptedInfo.getCarPeccancy() == 2) {
						viewHelper.setCarPeccancyName("有违章未处理");
					}
					viewHelper.setCy_printTime(transferAcceptedInfo.getCy_printTime());
					if(StringUtil.isEmpty(transferAcceptedInfo.getVendorCode()) && StringUtil.isEmpty(transferAcceptedInfo.getModelCode())) {
						viewHelper.setVendor_model_Code("");
					} else {
						viewHelper.setVendor_model_Code(transferAcceptedInfo.getVendorCode()+"-"+transferAcceptedInfo.getModelCode());
					}
					if(StringUtil.isNumber(transferAcceptedInfo.getCarType())) {
						viewHelper.setCarType(code.getItemValueByItemIdAndItemCode(Codes.VEHICLE_TYPE, Integer.parseInt(transferAcceptedInfo.getCarType())));
					} else {
						viewHelper.setCarType("");
					}
					if(StringUtil.isNumber(transferAcceptedInfo.getCountryStyle())) {
						viewHelper.setCountryStyle(code.getItemValueByItemIdAndItemCode(Codes.VEHICLE_COUNTRY, Integer.parseInt(transferAcceptedInfo.getCountryStyle())));
					} else {
						viewHelper.setCountryStyle("");
					}
					if(StringUtil.isNumber(transferAcceptedInfo.getCarUse())) {
						viewHelper.setCarUse(code.getItemValueByItemIdAndItemCode(Codes.USER_NATURE, Integer.parseInt(transferAcceptedInfo.getCarUse())));
					} else {
						viewHelper.setCarUse("");
					}
					viewHelper.setCardType(code.getItemValueByItemIdAndItemCode(Codes.PLATE_TYPE, transferAcceptedInfo.getCardType()));
					viewHelper.setRegisterCardNo(transferAcceptedInfo.getRegisterCardNo());
					viewHelper.setRegDate(DateUtil.DateToString11(transferAcceptedInfo.getRegDate()));
					viewHelper.setGainType(code.getItemValueByItemIdAndItemCode(Codes.GAIN_TYPE, transferAcceptedInfo.getGainType()));
					viewHelper.setPrice(StringUtil.addDH(transferAcceptedInfo.getPrice()+""));
					viewHelper.setNewCarOwner(transferAcceptedInfo.getNewcarOwner());
					viewHelper.setNewOwnerPhone(transferAcceptedInfo.getNewOwnerPhone());
					viewHelper.setNewOwnerAddress(transferAcceptedInfo.getNewOwnerAddress());
					viewHelper.setNewAgentOwnerIdCardNo(transferAcceptedInfo.getNewAgentOwnerIdCardNo());
					viewHelper.setNewAgentName(transferAcceptedInfo.getNewAgentName());
					viewHelper.setNewAgentPhone(transferAcceptedInfo.getNewAgentPhone());
					viewHelper.setNewAgentAddress(transferAcceptedInfo.getNewAgentAddress());
					viewHelper.setNewOwnerZipCode(transferAcceptedInfo.getNewOwnerZipCode());
					viewHelper.setNewCarOwnerId_Org_No(transferAcceptedInfo.getNewOwnerType()==1?transferAcceptedInfo.getNewcarOwnerIdCardNo():transferAcceptedInfo.getNewOwnerOrgNo());
				}
			}
			
			uc.setAttribute(TmsConstants.VIEWHELP, viewHelper);

			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
		logger.debug(this.getClass().getName() + "#execute was completed.");

	}
}
